﻿using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DevSamPle.FormPlugin
{
    [Description("【表单插件】BeforeControlDataBinder 单据查看界面，对某些字段的值进行特殊处理"), HotUpdate]
    public class CustBeforeControlDataBinder : AbstractDynamicFormPlugIn
    {
        public override void BarItemClick(BarItemClickEventArgs e)
        {
            base.BarItemClick(e);
            if (e.BarItemKey.EqualsIgnoreCase("BeforeControlDataBinder"))
            {
                //刷新字段时也会触发BeforeControlDataBinder事件
                this.View.UpdateView("F_POSW_TestText1");
                this.View.UpdateView("F_TEST_Decimal1");
            }
        }
        public override void BeforeControlDataBinder(BeforeControlDataBinder e)
        {
            base.BeforeControlDataBinder(e);
            if (e.Control.Key.EqualsIgnoreCase("F_POSW_TestText1"))
            {
                var value = this.Model.GetValue(e.Control.Key);
                e.BindValue = new JSONObject();
                e.BindValue[FieldAppearance.FS_VALUE] = string.Format("{0}-{1}", value, Context.UserName);
            }
            else if (e.Control.Key.EqualsIgnoreCase("F_TEST_Decimal1"))
            {
                var value = Convert.ToDecimal(this.Model.GetValue(e.Control.Key));
                var newValue = (value * 100).ToString();
                if (newValue.Contains('.'))
                {
                    newValue = newValue.TrimEnd('0').TrimEnd('.');
                }
                e.BindValue = new JSONObject();
                e.BindValue[FieldAppearance.FS_VALUE] = newValue;
            }
            else if (e.Control.Key.EqualsIgnoreCase("FPOOrderEntry"))
            {
                var dataObj = e.RowData;
                var value = this.Model.GetValue(e.Control.Key);
                e.BindValue = new JSONObject();
                e.BindValue[FieldAppearance.FS_VALUE] = string.Format("{0}-{1}", value, Context.UserName);
            }
        }
    }
}
